import 'package:bnotes/constants.dart';
import 'package:bnotes/helpers/database_helper.dart';
import 'package:bnotes/models/note_list_model.dart';
import 'package:bnotes/models/notes_model.dart';
import 'package:bnotes/widgets/note_edit_list_textfield.dart';
import 'package:bnotes/widgets/note_edit_title_textfield.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:uuid/uuid.dart';

class EditNotePage extends StatefulWidget {
  final Notes note;

  const EditNotePage({Key? key, required this.note}) : super(key: key);

  @override
  _EditNotePageState createState() => _EditNotePageState();
}

class _EditNotePageState extends State<EditNotePage> {
  final FocusNode titleFocusNode = FocusNode();
  final FocusNode contentFocusNode = FocusNode();
  TextEditingController _noteTitleController = new TextEditingController();
  TextEditingController _noteTextController = new TextEditingController();
  TextEditingController _noteListTextController = new TextEditingController();
  bool _noteListCheckValue = false;
  String currentEditingNoteId = "";
  String _noteListJsonString = "";
  final dbHelper = DatabaseHelper.instance;
  var uuid = Uuid();
  late Notes note;
  bool isCheckList = false;
  List<NoteListItem> _noteListItems = [];

  void _saveNote() async {
    if (currentEditingNoteId.isEmpty) {
      setState(() {
        note = new Notes(
            uuid.v1(),
            DateTime.now().toString(),
            _noteTitleController.text,
            _noteTextController.text,
            '',
            0,
            0,
            _noteListJsonString);
      });
      await dbHelper.insertNotes(note).then((value) {});
    } else {
      setState(() {
        note = new Notes(
            currentEditingNoteId,
            DateTime.now().toString(),
            _noteTitleController.text,
            _noteTextController.text,
            '',
            0,
            0,
            _noteListJsonString);
      });
      await dbHelper.updateNotes(note).then((value) {});
    }
  }

  void onSubmitListItem() async {
    _noteListItems.add(new NoteListItem(_noteListTextController.text, 'false'));
    _noteListTextController.text = "";
    print(_noteListCheckValue);
  }

  @override
  void initState() {
    super.initState();
    setState(() {
      note = widget.note;
      _noteTextController.text = note.noteText;
      _noteTitleController.text = note.noteTitle;
      currentEditingNoteId = note.noteId;
      isCheckList = note.noteList.contains('{');
    });
    titleFocusNode.requestFocus();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        actions: [
          InkWell(
            child: Icon(Icons.check),
            onTap: _onBackPressed,
          ),
          SizedBox(width: 15),
        ],
      ),
      body: GestureDetector(
        onTap: () {
          contentFocusNode.requestFocus();
        },
        child: Padding(
          padding: kGlobalOuterPadding,
          child: ListView(
            children: [
              Padding(
                padding: kGlobalOuterPadding,
                child: Container(
                  child: NoteEditTextField(
                    controller: _noteTitleController,
                    hint: '输入错题笔记标题',
                    focusNode: titleFocusNode,
                    onSubmitFocusNode: contentFocusNode,
                  ),
                ),
              ),
              Padding(
                padding: kGlobalOuterPadding,
                child: Container(
                  child: NoteEditTextField(
                    controller: _noteTextController,
                    hint: '此处输入笔记内容...',
                    focusNode: contentFocusNode,
                    isContentField: true,
                  ),
                ),
              ),
              if (isCheckList)
                ...List.generate(_noteListItems.length, generatenoteListItems),
              Visibility(
                visible: isCheckList,
                child: NoteEditListTextField(
                  checkValue: _noteListCheckValue,
                  controller: _noteListTextController,
                  onSubmit: () => onSubmitListItem(),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget generatenoteListItems(int index) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 12.0, vertical: 5.0),
      child: Row(
        children: [
          Icon(Icons.check_box),
          SizedBox(
            width: 5.0,
          ),
          Expanded(
            child: Text(_noteListItems[index].value),
          ),
        ],
      ),
    );
  }

  Future<bool> _onBackPressed() async {
    if (_noteTextController.text.isNotEmpty) {
      _saveNote();
      Navigator.pop(context, note);
    } else {
      Navigator.pop(context, false);
    }
    return false;
  }
}
